﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using University.Workshop.DataAccess;
using University.Workshop.Model;

namespace University.Workshop.Business
{
    public static class WebmasterRequestManager
    {
        public static WebmasterRequest RegisterNewRequest(string userEmail, string skills, bool wantsToReplyTickets, bool wantsToSetWebmasters)
        {
            User user = UserDataAccess.GetUserByEmail(userEmail);
            if (user != null)
            {
                if (skills == null)
                    throw new BusinessException("Skills description must be at least 200 chars!");
                if(skills.Length<200)
                    throw new BusinessException("Skills description must be at least 200 chars!");
                if(!wantsToReplyTickets && !wantsToSetWebmasters)
                    throw new BusinessException("You should select at least 1 job you would want to have!");
                WebmasterRequest webmasterReq = new WebmasterRequest
                                                    {
                                                        CandidateId = user.Id,
                                                        Skills = skills,
                                                        Date = DateTime.Now,
                                                        Status = WebmasterRequestStatus.Voting,
                                                        WantsToReplyTickets = wantsToReplyTickets,
                                                        WantsToSetWebmasters = wantsToSetWebmasters,
                                                        PositiveVotes = 0,
                                                        NegativeVotes = 0
                                                    };
                return WebmasterRequestDataAccess.InsertNewWebmasterRequest(webmasterReq);
            }
            else return null;
        }

        public static bool HavePendingRequest(string userEmail)
        {
            User user = UserDataAccess.GetUserByEmail(userEmail);
            if (user != null)
            {
                WebmasterRequest webmasterReq = WebmasterRequestDataAccess.GetWebmasterRequestByCandidateId(user.Id);
                return webmasterReq != null && webmasterReq.Status == WebmasterRequestStatus.Voting;
            }
            return false;
        }
    }
}
